package org.infodavid.common.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IDocumentVersionListItem;
import org.infodavid.common.impl.model.criteria.DocumentVersionCriteriaImpl;
import org.infodavid.common.model.DocumentVersionTest;
import org.infodavid.common.model.IDocumentVersion;
import org.infodavid.common.model.behaviors.IApprovable.EApprovalStatus;
import org.infodavid.common.model.criteria.IDocumentVersionCriteria;

/**
 * The Class DocumentDataServiceTest.
 */
@SuppressWarnings({
    "null", "boxing"
})
public class DocumentVersionDataServiceTest extends
    AbstractDataServiceTest<IDocumentVersionListItem,IDocumentVersion,Long> {

	/**
	 * The Constructor.
	 */
	public DocumentVersionDataServiceTest() {
		super(new DocumentVersionTest(), DocumentVersionDataService.getInstance());
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.AbstractDataServiceTest#modify(org.infodavid.common.model.IDataObject
	 * )
	 */
	@Override
	public void modify(final IDocumentVersion item) {
		item.setComment(item.getComment() + "modif" + RAND.nextInt(1000));
		item.setApprovalStatus(EApprovalStatus.OBSOLETE);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteria() throws Exception {
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();
		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingAllCriterionsl() throws Exception {
		final Collection<IDocumentVersion> inserted = getTestData();
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setNamePattern(inserted.iterator().next().getName());
		criteria.setRevision(inserted.iterator().next().getRevision());
		criteria.setDocumentKey(inserted.iterator().next().getDocument().getKey());

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", results.size(), count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameNoDataFound() throws Exception {
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setNamePattern("NoData");

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 0, results.size());
		Assert.assertEquals("Not all data has been found", 0, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingDocumentEqual() throws Exception {
		final Collection<IDocumentVersion> inserted = getTestData();
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setDocumentKey(inserted.iterator().next().getDocument().getKey());

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", results.size(), count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingRevisionEqual() throws Exception {
		final Collection<IDocumentVersion> inserted = getTestData();
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setRevision(inserted.iterator().next().getRevision());

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", results.size(), count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameEqual() throws Exception {
		final Collection<IDocumentVersion> inserted = getTestData();
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setNamePattern(inserted.iterator().next().getName());

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", results.size(), count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameLike() throws Exception {
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setNamePattern("item%");

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCollection() throws Exception {
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();
		Exception exception = null;

		try {
			((DocumentVersionDataService)getService()).find(getSession(), null, criteria);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCriteria() throws Exception {
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results,
		        (IDocumentVersionCriteria)null);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingPage() throws Exception {
		final List<IDocumentVersionListItem> results = new ArrayList<IDocumentVersionListItem>();
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setPageStart(Long.valueOf(0));
		criteria.setPageSize(Integer.valueOf(5));

		final long count =
		    ((DocumentVersionDataService)getService()).find(getSession(), results, criteria);

		// System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", criteria.getPageSize().intValue(),
		    results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindLastByName() throws Exception {
		final List<IDocumentVersion> inserted = getTestData();
		final IDocumentVersion version = inserted.listIterator(inserted.size()).previous();
		final IDocumentVersion result =
		    ((DocumentVersionDataService)getService()).findLastVersionByName(getSession(),
		        version.getName());

		Assert.assertNotNull("Result is null", result);
		Assert.assertEquals("Wrong data found", version.getName(), result.getName());
		Assert.assertEquals("Wrong data found", version.getKey(), result.getKey());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindLastByNameUsingNull() throws Exception {
		Exception exception = null;

		try {
			((DocumentVersionDataService)getService()).findLastVersionByName(getSession(), null);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindLastRevision() throws Exception {
		final List<IDocumentVersion> inserted = getTestData();
		final IDocumentVersion version = inserted.listIterator(inserted.size()).previous();
		final Integer result =
		    ((DocumentVersionDataService)getService()).findLastRevision(getSession(), version
		        .getDocument().getKey());

		Assert.assertNotNull("Result is null", result);
		Assert.assertEquals("Wrong data found", version.getRevision(), result.intValue());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindLastRevisionUsingNull() throws Exception {
		Exception exception = null;

		try {
			((DocumentVersionDataService)getService()).findLastRevision(getSession(), null);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("Exception thrown", exception);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindLastVersion() throws Exception {
		final List<IDocumentVersion> inserted = getTestData();
		final IDocumentVersion version = inserted.listIterator(inserted.size()).previous();
		final IDocumentVersion result =
		    ((DocumentVersionDataService)getService()).findLastVersion(getSession(), version
		        .getDocument().getKey());

		Assert.assertNotNull("Result is null", result);
		Assert.assertEquals("Wrong data found", version.getKey(), result.getKey());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindLastVersionUsingNull() throws Exception {
		Exception exception = null;

		try {
			((DocumentVersionDataService)getService()).findLastVersion(getSession(), null);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("Exception thrown", exception);
	}

	/**
	 * Test get instance.
	 */
	@Test
	public final void testGetInstance() {
		Assert.assertEquals("Not a singleton", DocumentVersionDataService.getInstance(),
		    DocumentVersionDataService.getInstance());
	}
}
